	package br.unb.mestrado.simulator.ui;
import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

import script.Resources;
import br.unb.mestrado.simulator.ui.tab.TabGoal;
import br.unb.mestrado.simulator.ui.tab.TabMultiCriteria;
import br.unb.mestrado.simulator.ui.tab.TabSensors;
import br.unb.mestrado.simulator.ui.tab.TabSimulation;

public class SimulatorWindow extends Composite {

	protected static final int SIMULATION_INDEX = 3;
	
	private TabSensors sensorsTabControl;
	private TabMultiCriteria multiCriteriaTabControl;
	private TabSimulation simulationTabControl;
	private TabGoal goalTabControl;
	
	public SimulatorWindow(Shell shell) {
		super(shell, SWT.NONE);
		this.configureUI();
	}

	private void configureUI() {
		setLayout(new GridLayout(1, true));
		this.configureTab();
	}

	private void configureTab() {
		final TabFolder tabFolder = new TabFolder(this, SWT.NONE);
		GridData gridData = new GridData(GridData.FILL, GridData.FILL, true,true);
		gridData.horizontalSpan = 2;
		gridData.horizontalAlignment = SWT.FILL;
		
		this.sensorsTabControl = new TabSensors(tabFolder, SWT.NONE);
		TabItem sensorsTab = new TabItem(tabFolder, SWT.NONE);
		sensorsTab.setText("Sensors");
		sensorsTab.setControl(sensorsTabControl);
		
		this.multiCriteriaTabControl = new TabMultiCriteria(tabFolder, SWT.NONE);
		TabItem multiCriteriaTab = new TabItem(tabFolder, SWT.NONE);
		multiCriteriaTab.setText("Multi-criteria");
		multiCriteriaTab.setControl(multiCriteriaTabControl);
		
		this.goalTabControl = new TabGoal(tabFolder, SWT.NONE);
		TabItem goalProgrammingTab = new TabItem(tabFolder, SWT.NONE);
		goalProgrammingTab.setText("Goal-oriented");
		goalProgrammingTab.setControl(goalTabControl);
		
		this.simulationTabControl = new TabSimulation(tabFolder, SWT.NONE);
		TabItem simulationTab = new TabItem(tabFolder, SWT.NONE);
		simulationTab.setText("Simulation");
		simulationTab.setControl(simulationTabControl);

		tabFolder.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(e.getSource() instanceof TabFolder){
					TabFolder tabFolder = (TabFolder) e.getSource();
					int index = tabFolder.getSelectionIndex();
					if(index == SIMULATION_INDEX){
						simulationTabControl.updateConfigurations();
						simulationTabControl.updateFormulas();
					}
				}
			}
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				widgetDefaultSelected(e);
			}
		});
		
		tabFolder.setBackground(new Color(Display.getCurrent(), new RGB(0, 255,0)));
		tabFolder.setLayoutData(gridData);
		tabFolder.pack();
		tabFolder.update();
		
		tabFolder.setSelection(0);
	}


    
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setSize(850, 650);
		shell.setText("Simulator");
		shell.setLayout( new FillLayout());
//		try {
			
			InputStream is = Resources.class.getResourceAsStream("app.png");//new FileInputStream(new File("lib/app.png"));
			if(is!=null){
				Image image = new Image(display, is);
				shell.setImage(image);
			}
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		}

		new SimulatorWindow(shell);
//		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

}
